package com.hoshiicloud.saleorder.controller.customer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.saleorder.param.customer.ReviewsImagesAddParam;
import com.hoshiicloud.saleorder.param.customer.ReviewsImagesQueryParam;
import com.hoshiicloud.saleorder.param.customer.ReviewsImagesUpdateParam;
import com.hoshiicloud.saleorder.service.customer.IReviewsImagesService;
import com.hoshiicloud.user.entity.customer.ReviewsImages;
import com.hoshiicloud.user.vo.customer.ReviewsImagesDetailVO;
import com.hoshiicloud.user.vo.customer.ReviewsImagesVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 平台商品评价-图片及视频 前端控制器
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-28
 */
@Api(value = "前台用户操作", tags = {"平台商品评价-图片及视频管理"})
@Slf4j
@Validated
@RestController
@RequestMapping("/customer/reviews-images")
public class ReviewsImagesController {

    @Autowired
    private IReviewsImagesService reviewsImagesService;

    @ApiOperation(value = "新增平台商品评价-图片及视频", notes = "")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Response add(@RequestBody @Valid ReviewsImagesAddParam param) {
        ReviewsImages entity = new ReviewsImages();
        BeanUtils.copyProperties(param, entity);
        // TODO 只接受指定参数
        return reviewsImagesService.insert(entity);
    }

    @ApiOperation(value = "修改平台商品评价-图片及视频", notes = "")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Response update(@RequestBody @Valid ReviewsImagesUpdateParam param) {
        ReviewsImages entity = new ReviewsImages();
        BeanUtils.copyProperties(param, entity);
        // TODO 只接受指定参数
        return reviewsImagesService.updateDataById(entity);
    }

    @ApiOperation(value = "删除平台商品评价-图片及视频", notes = "")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response delete(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            return Response.failed("id不能为空");
        }
        return reviewsImagesService.deleteDataById(id);
    }

    @ApiOperation(value = "获取平台商品评价-图片及视频详情", notes = "")
    @RequestMapping(value = "/getDetail", method = RequestMethod.GET)
    public Response<ReviewsImagesDetailVO> getDetail(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            return Response.failed("id不能为空");
        }
        return reviewsImagesService.getDetail(id);
    }

    @ApiOperation(value = "获取平台商品评价-图片及视频列表", notes = "")
    @RequestMapping(value = "getList", method = RequestMethod.GET)
    public Response<List<ReviewsImagesVO>> getList(@RequestBody @Valid ReviewsImagesQueryParam param) {
        QueryWrapper<ReviewsImages> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(param.getId() != null, ReviewsImages::getId, param.getId())
                .eq(param.getReviewId() != null, ReviewsImages::getReviewId, param.getReviewId())
                .eq(StringUtils.isNotBlank(param.getThumbnail()), ReviewsImages::getThumbnail, param.getThumbnail())
                .eq(StringUtils.isNotBlank(param.getImage()), ReviewsImages::getImage, param.getImage());
        String key = "ReviewsImagesQueryParam{" + param.toString() + "}, Page{pageNumber=" + param.getPageNumber()
                + ", pageSize=" + param.getPageSize() + "}";
        return reviewsImagesService.getList(queryWrapper, key);
    }

    @ApiOperation(value = "分页查询平台商品评价-图片及视频", notes = "")
    @RequestMapping(value = "/getPage", method = RequestMethod.GET)
    public Response<IPage<ReviewsImagesVO>> getPage(@RequestBody @Valid ReviewsImagesQueryParam param) {
        QueryWrapper<ReviewsImages> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(param.getId() != null, ReviewsImages::getId, param.getId())
                .eq(param.getReviewId() != null, ReviewsImages::getReviewId, param.getReviewId())
                .eq(StringUtils.isNotBlank(param.getThumbnail()), ReviewsImages::getThumbnail, param.getThumbnail())
                .eq(StringUtils.isNotBlank(param.getImage()), ReviewsImages::getImage, param.getImage());
        IPage<ReviewsImages> page = new Page<>(Long.parseLong(param.getPageNumber()),
                Long.parseLong(param.getPageSize()));
        String key = "ReviewsImagesQueryParamToPage{" + param.toString() + "}, Page{pageNumber=" + param.getPageNumber()
                + ", pageSize=" + param.getPageSize() + "}";
        return reviewsImagesService.getPage(page, queryWrapper, key);
    }
}
